<%@ page import="org.xilin.Course; org.xilin.Family" %>

<g:setProvider library="jquery"/>
  <tr>
    <th>Student Name</th>
    <th>Class Name</th>
    <g:set var="counter" value="${classes.size()}" />
    <g:each in="${classes}" var="clazz">
        <th>${Course.read(clazz.courseId)?.name}</th>
    </g:each>
    <g:while test="${counter++ < 4}">
        <th></th>
    </g:while>
    <th>Class Total</th>
  </tr>
  <tr>
    <td>${student.firstName} ${student.lastName}</td>
    <th class="spec">Price</th>
    <g:set var="counter" value="${classes.size()}" />
    <g:set var="classTotal" value="${0}" />
    <g:set var="bookList" value="${[]}" />
    <g:each in="${classes}" var="clazz">
        <%
          def course = Course.read(clazz.courseId)

          def price = course?.price
          classTotal += price
          // add book id here
          bookList << course.book.id
        %>
        <td>${Course.read(clazz.courseId)?.price}</td>
    </g:each>
    <g:while test="${counter++ < 4}">
        <td></td>
    </g:while>
    <td class="spec">${classTotal}</td>
  </tr>
  <tr>
    <th class="nobg"></th>
    <th>Book</th>
    <g:set var="counter" value="${books.size()}" />
    <g:each in="${books}" var="book">
        <th>${book.name}</th>
    </g:each>
    <g:while test="${counter++ < 4}">
        <th></th>
    </g:while>
    <th>Book Total</th>
  </tr>
  <tr>
    <th class="nobg"></th>
    <th class="spec">Price</th>
    <g:set var="counter" value="${books.size()}" />
    <g:set var="bookTotal" value="${0}" />
    <g:each in="${books}" var="book">
        <%
          bookTotal += book.price
        %>
        <td>${book.price}</td>
    </g:each>
    <g:while test="${counter++ < 4}">
        <td></td>
    </g:while>
    <td class="spec">${bookTotal}</td>
  </tr>
  <tr>
    <th class="nobg"></th>
    <th class="spec">Extra Fee</th>
    <g:set var="counter" value="${books.size()}" />
    <g:set var="bookExtraTotal" value="${0}" />
    <g:each in="${books}" var="book">
        <%
          bookExtraTotal += bookList.contains(book.id) ? 0 : book.extraFee
        %>
        <td>${bookList.contains(book.id) ? 0 : book.extraFee}</td>
    </g:each>
    <g:while test="${counter++ < 4}">
        <td></td>
    </g:while>
    <td class="spec">${bookExtraTotal}</td>
  </tr>
  <tr>
    <td class="nobg" colspan="5"></td>
    <th class="specalt">Total Cost</th>
    <td class="totalCost">${bookTotal + bookExtraTotal + classTotal}</td>
  </tr>
